﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Models.DataModel;
using System.Data.Entity;

namespace Models.DataStore
{
    public partial class Ctrl : Core
    {
        public List<Country> GetListCountry(String temp = "")
        {
            if (temp!=null && temp.Length > 0)
                return db.Countries.Where(ct => ct.Name.Contains(temp)).ToList();
            else
                return db.Countries.ToList();
        }
        /// <summary>
        /// them moi country
        /// </summary>
        /// <param name="country"></param>
        /// <returns></returns>
        public async Task<int> AddNewCountry(Country country)
        {
            try
            {
                country.CountryId = Guid.NewGuid();
                db.Entry(country).State = EntityState.Added;
                return await db.SaveChangesAsync();
            }
            catch { return 0; }
        }
        /// <summary>
        /// lay len country theo ma id Guid
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Country GetCountryById(Guid id)
        {
            return db.Countries.Where(ct => ct.CountryId == id).FirstOrDefault();
        }
        /// <summary>
        /// ham sua country
        /// </summary>
        /// <param name="country"></param>
        public async Task<int> EditCountry(Country country)
        {
            try
            {
                db.Entry(country).State = EntityState.Modified;
                return await db.SaveChangesAsync();
            }
            catch
            {
                return 0;
            }
        }
        public bool CheckCodeCountry(String code)
        {
           var temp= db.Countries.FirstOrDefault(c => c.Code == code);
           if (temp == null)
               return true;
           return false;
        }
        /// <summary>
        /// xoa country theo id
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<int> DeleteCountry(Guid id)
        {
            if (CheckCountry(id))
            {
                Country country = db.Countries.FirstOrDefault(c => c.CountryId == id);
                db.Countries.Remove(country);
                return await db.SaveChangesAsync();
            }
            return 0;
        }

        /// <summary>
        /// tra ve true neu khong ton tai country trong bang Company va nguoc lai
        /// </summary>
        /// <param name="countryid"></param>
        /// <returns></returns>
        public bool CheckCountry(Guid countryid)
        {
            return db.Companies.Where(cp => cp.CountryId == countryid).FirstOrDefault() == null ? true : false;
        }
        /// <summary>
        /// xoa nhieu dong trong list
        /// </summary>
        /// <param name="countryItemDelete"></param>
        /// <returns></returns>
        /// 
        public async Task<int> DeleteSelectedCountry(IEnumerable<Guid> countryItemDelete)
        {
            try {
                List<Country> list = db.Countries.Where(cp => countryItemDelete.Contains(cp.CountryId)).ToList<Country>();
                db.Countries.RemoveRange(list);
                return await db.SaveChangesAsync();
            }
            catch { return 0; }
            
           
        }

    }
}
